﻿/**
 * Image class handles Images
*/
dynamic class Image extends MovieClip {
	
	// Constants
	var padding:Number = 10;
	var image_folder:String = "assets/uploads/";
	
	// Variables
	var image_xml:XML;
	var is_loaded:Boolean;
	var display_type:String;
	var image_listener:Object;
	var image_loader:MovieClipLoader;
	var image_url:String;
	
	/**
	 * Image() instantiates the Image class
	*/
	function Image() {
		
		// Set loaded to false
		this.is_loaded = false;
		
		// Initialise empty XML
		this.image_xml = new XML("<image display=\"block\"><source></source><alt></alt><link target=\"_self\"></link><caption></caption><width></width><height></height></image>");
		
		// Load Background
		this.attachMovie("element_background", "background_mc", this.getNextHighestDepth(), {
			useHandCursor: false
		});
		
		// Add menu
		this.attachMovie("element_menu", "element_menu_mc", this.getNextHighestDepth());
		
		// Add border
		this.attachMovie("element_border", "element_border_mc", this.getNextHighestDepth());

		// focus image element when selected
		this.background_mc.onRelease = function() {
			_parent._parent.setElementFocus(_parent);
		}
		
		// Set up listener
		this.image_listener = new Object();
		this.image_loader = new MovieClipLoader();
		
		// On Load
		this.image_listener.onLoadInit = function(target_mc:MovieClip) {
			
			// Set loaded
			target_mc._parent.setLoaded(true);
			
			// Redraw
			_root.content_scrollpane.content.drawLayout();
		}
		
		// Error
		this.image_listener.onLoadError = function(target_mc:MovieClip, errorCode:String) {
			addOutput("An error occured when loading an image: " + errorCode + "<br>Image: " + this.image_url + "<br>Target: " + target_mc, "error");
			
			// Set loaded
			target_mc._parent.setLoaded(true);
			
			// Redraw
			_root.content_scrollpane.content.drawLayout();
		}
		
		// Attach listener
		this.image_loader.addListener(this.image_listener);
	}

	/**
	 * setLoaded() sets the loaded status
	 *
	 * @param loaded is the value of is_loaded
	*/
	function setLoaded(loaded:Boolean):Void {
		this.is_loaded = loaded;
	}

	/**
	 * isLoaded() returns the value of is_loaded
	 *
	 * @return the value
	*/
	function isLoaded():Boolean {
		return this.is_loaded;
	}
	
	/**
	 * loadImage() loads the image
	*/
	function loadImage():Void {
		
		// Set loaded Off
		this.setLoaded(false);
		
		// Generate URL
		this.image_url = _root.HOST_URL + this.image_folder + this.getSrc();
		
		// Create holder
		this.createEmptyMovieClip("image_mc", this.getNextHighestDepth());
		
		// Load the Clip
		image_loader.loadClip(this.image_url, this.image_mc);
	}

	/**
	 * getSource() gets the source
	 *
	 * @return the source
	*/
	function getSource():String {
		return this.image_xml.toString();
	}
	
	/**
	 * setSource() sets the source
	 *
	 * @param new_image_xml is the new image XML
	*/
	function setSource(new_image_xml:String):Void {
		
		// Set XML
		this.image_xml = new XML(new_image_xml);
	}

	/**
	 * displayDisplayType() updates the display type
	*/
	function displayDisplayType():Void {
		
		this.element_menu_mc.inline_cb_mc.setCheckMode((this.image_xml.firstChild.attributes.display == "inline") ? 0 : 2);
	}
	
	/**
	 * redraw() redraws the image element
	*/
	function redraw(column_width:Number):Void {
		
		// If image is bigger than column, draw to image size
		if (this.image_mc._width > column_width) {
			column_width = this.image_mc._width + 2;
		}
		
		// Image
		this.image_mc._y = this.element_menu_mc._height + 1;
		this.image_mc._x = 1;
		
		// Menu
		this.element_menu_mc.background_mc._width = column_width;
		
		// Background
		this.background_mc._width = column_width;
		this.background_mc._height = this.element_menu_mc._height + this.image_mc._height + 2;
		
		// Border
		this.element_border_mc._width = column_width;
		this.element_border_mc._height = this.element_menu_mc._height + this.image_mc._height + 2;

		// Make visible
		this._visible = true;
		
		// Update display type
		this.displayDisplayType();
	}
	
	/**
	 * focusElement() focuses this element
	*/
	function focusElement():Void {
		_root.focusElement(this);
	}
	
	/**
	 * blurElement blurs this element
	*/
	function blurElement():Void {
		_root.blurElement(this);
	}

	function getSrc() {
		if (this.image_xml.firstChild.firstChild.firstChild.nodeValue != null) {
			return this.image_xml.firstChild.firstChild.firstChild.nodeValue;
		} else {
			return "";			
		}
	}
	
	function setSrc(strSrc) {
		if (this.image_xml.firstChild.firstChild.firstChild.nodeValue != null) {
			this.image_xml.firstChild.firstChild.firstChild.nodeValue = strSrc;
		} else {
			var source_xmlNode:XMLNode = this.image_xml.createTextNode(strSrc);
			this.image_xml.firstChild.firstChild.appendChild(source_xmlNode);
		}
	}
	
	function getAlt() {
		if (this.image_xml.firstChild.firstChild.nextSibling.firstChild.nodeValue != null) {
			return this.image_xml.firstChild.firstChild.nextSibling.firstChild.nodeValue;
		} else {
			return "";			
		}
	}
	
	function setAlt(strAlt) {
		if (this.image_xml.firstChild.firstChild.nextSibling.firstChild.nodeValue != null) {
			this.image_xml.firstChild.firstChild.nextSibling.firstChild.nodeValue = strAlt;
		} else {
			var alt_xmlNode:XMLNode = this.image_xml.createTextNode(strAlt);
			this.image_xml.firstChild.firstChild.nextSibling.appendChild(alt_xmlNode);
		}
	}
	
	function getLink() {
		if (this.image_xml.firstChild.firstChild.nextSibling.nextSibling.firstChild.nodeValue != null) {
			return this.image_xml.firstChild.firstChild.nextSibling.nextSibling.firstChild.nodeValue;
		} else {
			return "";			
		}
	}
	
	function setLink(strLink) {
		if (this.image_xml.firstChild.firstChild.nextSibling.nextSibling.firstChild.nodeValue != null) {
			this.image_xml.firstChild.firstChild.nextSibling.nextSibling.firstChild.nodeValue = strLink;
		} else {
			var link_xmlNode:XMLNode = this.image_xml.createTextNode(strLink);
			this.image_xml.firstChild.firstChild.nextSibling.nextSibling.appendChild(link_xmlNode);
		}
	}
	
	function getTarget() {
		return this.image_xml.firstChild.firstChild.nextSibling.nextSibling.attributes.target;
	}
	
	function setTarget(strTarget) {
		this.image_xml.firstChild.firstChild.nextSibling.nextSibling.attributes.target = strTarget;
	}
	
	function getCaption() {
		
		if (this.image_xml.firstChild.firstChild.nextSibling.nextSibling.nextSibling.firstChild.nodeValue != null) {
			return this.image_xml.firstChild.firstChild.nextSibling.nextSibling.nextSibling.firstChild.nodeValue;
		} else {
			return "";			
		}
	}
	
	function setCaption(strCaption) {
		
		if (this.image_xml.firstChild.firstChild.nextSibling.nextSibling.nextSibling.firstChild.nodeValue != null) {
			this.image_xml.firstChild.firstChild.nextSibling.nextSibling.nextSibling.firstChild.nodeValue = strCaption;
		} else {
			var caption_xmlNode:XMLNode = this.image_xml.createTextNode(strCaption);
			this.image_xml.firstChild.firstChild.nextSibling.nextSibling.nextSibling.appendChild(caption_xmlNode);
		}
	}
	
	function getHTMLWidth() {
		/*if (this.image_xml.attributes.width) {
			return this.image_xml.attributes.width;
		} else {
			return "";
		}*/
		
		if (this.image_xml.firstChild.firstChild.nextSibling.nextSibling.nextSibling.nextSibling.firstChild.nodeValue != null) {
			return this.image_xml.firstChild.firstChild.nextSibling.nextSibling.nextSibling.nextSibling.firstChild.nodeValue;
		} else {
			return "";			
		}
	}
	
	function setHTMLWidth(new_width) {
		//this.image_xml.attributes.width = new_width;
		
		if (this.image_xml.firstChild.firstChild.nextSibling.nextSibling.nextSibling.nextSibling.firstChild.nodeValue != null) {
			this.image_xml.firstChild.firstChild.nextSibling.nextSibling.nextSibling.nextSibling.firstChild.nodeValue = new_width;
		} else {
			var width_xmlNode:XMLNode = this.image_xml.createTextNode(new_width);
			this.image_xml.firstChild.firstChild.nextSibling.nextSibling.nextSibling.nextSibling.appendChild(width_xmlNode);
		}
	}
	
	function getHTMLHeight() {
		/*if (this.image_xml.attributes.height) {
			return this.image_xml.attributes.height;
		} else {
			return "";
		}*/
		
		if (this.image_xml.firstChild.firstChild.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling.firstChild.nodeValue != null) {
			return this.image_xml.firstChild.firstChild.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling.firstChild.nodeValue;
		} else {
			return "";			
		}
	}
	
	function setHTMLHeight(new_height) {
		//this.image_xml.attributes.height = new_height;
		trace("setHTMLHeight(" + new_height + ")");
		
		if (this.image_xml.firstChild.firstChild.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling.firstChild.nodeValue != null) {
			trace("Already exists, modding");
			this.image_xml.firstChild.firstChild.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling.firstChild.nodeValue = new_height;
		} else {
			var height_xmlNode:XMLNode = this.image_xml.createTextNode(new_height);
			trace("Creating new node: " + height_xmlNode);
			this.image_xml.firstChild.firstChild.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling.appendChild(height_xmlNode);
		}
		
		trace("Saved: " + this.image_xml);
	}
	
	function getMinimumWidth() {
		if (this.image_mc._width > 280) {
			return this.image_mc._width + 2;
		} else {
			return 280;
		}
	}
	
	function toggleDisplayType() {
		if (this.image_xml.firstChild.attributes.display == "inline") {
			this.image_xml.firstChild.attributes.display = "block";
		} else {
			this.image_xml.firstChild.attributes.display = "inline";
		}
		
		// Update
		this.displayDisplayType();
	}
	
	/**
	 * isImage returns true
	 *
	 * @return true
	*/
	function isImage() {
		return true;
	}
}